package com.gitee.cui.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.gitee.cui.springcloud.entities.CommonResult;
import com.gitee.cui.springcloud.entities.Payment;
import com.gitee.cui.springcloud.handler.CustomBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022-03-19 09:10
 **/
@RestController
public class RateLimitController {

	@GetMapping("/byResource")
	@SentinelResource(value = "byResource", blockHandler = "byResourceHandler")
	public CommonResult<Payment> byResource() {
		return new CommonResult<>(200, "按资源名称限流测试OK", new Payment(2020L, "serial01"));
	}

	public CommonResult<Payment> byResourceHandler(BlockException e) {
		return new CommonResult<>(444, e.getClass().getCanonicalName() + "\t服务不可用");
	}

	@GetMapping("/rateLimit/byUrl")
	@SentinelResource(value = "byUrl")
	public CommonResult<Payment> byUrl() {
		return new CommonResult<>(200, "按URL限流测试OK", new Payment(2020L, "serial02"));
	}

	@GetMapping("/rateLimit/customBlockHandler")
	@SentinelResource(value = "customBlockHandler",
			blockHandlerClass = CustomBlockHandler.class, blockHandler = "handlerException2")
	public CommonResult customBlockHandler() {
		return new CommonResult(200, "按用户自定义限流OK", new Payment(2020L, "serial03"));
	}
}
